import json

def detect_jsonl_format(file_path):
    """
    检测并验证指定的JSONL文件格式。
    - 检查每行是否为有效JSON。
    - 检查是否包含指定的键。
    - 检查 'output' 字段是否为有效的JSON字符串。
    - 将所有检测结果格式化为一个JSON对象并返回。
    """

    # 初始化用于存储结果的字典
    results = {
        "file_path": file_path,
        "status": "Pending",
        "summary": {
            "total_lines_checked": 0,
            "valid_lines": 0,
            "invalid_lines": 0,
        },
        "errors": []
    }

    required_keys = {"instruction", "input", "output"}

    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            for i, line in enumerate(f):
                line_number = i + 1
                line_content = line.strip()

                # 跳过空行
                if not line_content:
                    continue

                results["summary"]["total_lines_checked"] += 1
                is_line_valid = True

                # 1. 检测该行是否为有效JSON
                try:
                    data = json.loads(line_content)
                except json.JSONDecodeError as e:
                    results["errors"].append({
                        "line_number": line_number,
                        "error_type": "Invalid JSON Format",
                        "details": str(e),
                        "line_content": line_content
                    })
                    is_line_valid = False
                    continue  # 无需进一步检测此行

                # 2. 检测是否包含所有必需的键
                missing_keys = required_keys - set(data.keys())
                if missing_keys:
                    results["errors"].append({
                        "line_number": line_number,
                        "error_type": "Missing Required Keys",
                        "details": f"缺少键: {list(missing_keys)}",
                        "line_content": line_content
                    })
                    is_line_valid = False

                # 3. 检测 'output' 字段是否为有效的JSON字符串（列表格式）
                if 'output' in data:
                    if not isinstance(data['output'], str):
                        results["errors"].append({
                            "line_number": line_number,
                            "error_type": "Incorrect Type for 'output'",
                            "details": f"'output' 字段应为字符串，但检测到类型: {type(data['output']).__name__}",
                            "line_content": line_content
                        })
                        is_line_valid = False
                    else:
                        try:
                            output_data = json.loads(data['output'])
                            if not isinstance(output_data, list):
                                results["errors"].append({
                                    "line_number": line_number,
                                    "error_type": "Invalid Nested JSON Type",
                                    "details": f"'output' 字段解析后应为列表，但检测到类型: {type(output_data).__name__}",
                                    "line_content": line_content
                                })
                                is_line_valid = False
                        except json.JSONDecodeError as e:
                            results["errors"].append({
                                "line_number": line_number,
                                "error_type": "Invalid Nested JSON in 'output'",
                                "details": str(e),
                                "line_content": line_content
                            })
                            is_line_valid = False

                # 更新统计
                if is_line_valid:
                    results["summary"]["valid_lines"] += 1

            # 统计无效行总数并更新最终状态
            results["summary"]["invalid_lines"] = len(results["errors"])
            if results["summary"]["invalid_lines"] == 0:
                results["status"] = "Success: All lines are valid."
            else:
                results["status"] = f"Failed: Found {results['summary']['invalid_lines']} invalid line(s)."

    except FileNotFoundError:
        results["status"] = "Error: File not found."
        results["errors"].append({
            "error_type": "File Not Found",
            "details": f"无法找到文件: '{file_path}'"
        })
    except Exception as e:
        results["status"] = "Error: An unexpected error occurred."
        results["errors"].append({
            "error_type": "Unexpected Error",
            "details": str(e)
        })

    return results


# --- 主程序运行 ---
if __name__ == '__main__':
    # 调用检测函数
    FILENAME_TO_CHECK = "trian_data/training_data.jsonl"
    validation_results = detect_jsonl_format(FILENAME_TO_CHECK)
    # 将最终的检测结果以格式化的JSON形式打印出来
    # indent=4 使输出更美观
    # ensure_ascii=False 确保中文字符正常显示
    print(json.dumps(validation_results, indent=4, ensure_ascii=False))
